/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.safetytask.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.safetytask.service.api.JobsTasksService;
import com.koron.safetytask.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

@Api(tags = "jobsTasks", description = "工作任务接口")
@RestController
@RequestMapping("/jobsTasks")
public class JobsTasksController {
    @Autowired
    private JobsTasksService service;

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffCode", value = "人员code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "staffName", value = "人员name" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "taskType", value = "任务类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "router", value = "路由" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "任务状态" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "begin", value = "开始日期" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "end", value = "结束日期" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/query.htm", method = {RequestMethod.GET})
    public MessageBean<?> query(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String staffCode, String staffName, String taskType, String router, Integer state, @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin, @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        if (StringUtils.isEmpty(staffCode)) {
            staffCode = user.getCode();
        }
        return service.list(staffCode, staffName, taskType, router, state, begin, end, page, pageSize);
    }

    @ApiOperation("提交任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "nextLeaderCode", value = "任务审核人" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "note", value = "备注" , paramType = "query", dataType = "String")})
    @RequestMapping(value = "/submit.htm", method = {RequestMethod.POST})
    @Log(title = "工作任务完成提交", businessModule = "工作任务", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> submit(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id, String nextLeaderCode, String title, String note, MultipartFile file) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        return service.submit(user, id, nextLeaderCode, title, note, file);
    }

    @ApiOperation("任务详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id" , paramType = "query", dataType = "String")})
    @RequestMapping(value = "/details.htm", method = {RequestMethod.GET})
    public MessageBean<?> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        return service.details(id);
    }
}
